package com.sky.controller.user;

import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersRejectionDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.entity.Orders;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrdersService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Description TODO
 * @Author itheima
 * @Date 2023-07-20 18:38
 */
@RestController("userOrdersController")
@Slf4j
@Api(tags = "订单接口")
@RequestMapping("/user/order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;

    /**
     * 用户下单相关接口
     * @return
     */
    @ApiOperation("用户下单相关接口")
    @PostMapping("/submit")
    public Result<OrderSubmitVO>  submit(@RequestBody OrdersSubmitDTO ordersSubmitDTO){
        log.info("用户下单相关接口:{}",ordersSubmitDTO);

        OrderSubmitVO orderSubmitVO=ordersService.submit(ordersSubmitDTO);
        return  Result.success(orderSubmitVO);
    }
    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = ordersService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    /**
     * 根据id查询订单详情
     * @param id
     * @return
     */
    @ApiOperation("查询订单详情")
    @GetMapping("/orderDetail/{id}")
    public  Result<Orders>  findByorderId(@PathVariable Long id){
        log.info("根据id查询订单详情id:{}",id);
        OrderVO orders=ordersService.findByorderId(id);
        return Result.success(orders);
    }

    /**
     * 查看历史订单
     * @param ordersPageQueryDTO
     * @return
     */
    @GetMapping("/historyOrders")
    @ApiOperation("查询历史订单")
    public Result<PageResult> historyOrders(OrdersPageQueryDTO ordersPageQueryDTO){
        log.info("查询历史订单:{}",ordersPageQueryDTO);
        PageResult pageResult=ordersService.page(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 根据id再来一单
     * @param id
     * @return
     */
    @ApiOperation("再来一单")
    @PostMapping("/repetition/{id}")
    public Result  repetitionid(@PathVariable Long id){
        log.info("再来一单id:{}",id);
        ordersService.repetitionid(id);
        return Result.success();
    }

    /**
     * 取消订单
     * @param id
     * @return
     */
    @ApiOperation("取消订单")
    @PutMapping("/cancel/{id}")
    public  Result  cancelid(@PathVariable Long id) throws Exception {
        log.info("取消订单:{}",id);
        Orders orders =new Orders();
        orders.setId(id);
        ordersService.updateid(orders);
        return Result.success();
    }

    /**
     * 用户催单
     * @param id
     * @return
     */
    @ApiOperation("用户催单")
    @GetMapping("reminder/{id}")
    public Result reminderId(@PathVariable Long id) {
        log.info("用户催单");
        ordersService.reminderid(id);
        return Result.success();
    }
}
